'''
Author: BujJieLi chengjie.li1@pacteraedge.com
Date: 2023-01-18 15:09:37
LastEditors: BujJieLi chengjie.li1@pacteraedge.com
LastEditTime: 2023-01-19 10:29:28
FilePath: /Dwarf/dwarf_debugger/ui/dialogs/dialog_disass_arch.py
Description: 

Copyright (c) 2023 by BujJieLi chengjie.li1@pacteraedge.com, All Rights Reserved. 
'''
from dwarf_debugger.ui.dialogs.dwarf_dialog import DwarfDialog
from dwarf_debugger.config import DwarfConfig, DisassemblyArch

from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QGroupBox, QRadioButton

class DisassemblyArchRadioDialog(DwarfDialog):
    '''
    用于配置反汇编时对应反汇编架构
    
    '''
    choice = DisassemblyArch.DisassArch_Arm32
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.main_app = parent
        self.support_arch = ['Arm32', 'Arm64', 'x86', 'x86_64']
        self.radio_list:list[QRadioButton] = []
        g_box = QGroupBox(self)
        v_box = QVBoxLayout(g_box)
        for i in self.support_arch:
            radio_btn = QRadioButton(i)
            radio_btn.toggled.connect(self.__btnState)
            v_box.addWidget(radio_btn)
            self.radio_list.append(radio_btn)
        self.radio_list[int(DisassemblyArchRadioDialog.choice)].setChecked(True) ## Arm32 -> default
        
        g_box.setLayout(v_box)
        g_box.resize(100,200)
    
    def __btnState(self):
        try:
            for btn in self.radio_list:
                _index  = self.support_arch.index(btn.text())
                if btn.isChecked():
                    # DisassemblyArchRadioDialog.choice = DisassemblyArch(_index)
                    DwarfConfig.disassembly_arch = DisassemblyArch(_index)
                    if(self.main_app != None):
                        address = self.main_app.dwarf.disassembler.cur_address
                        if(address != 0):
                            self.main_app.debug_panel.jump_to_address(address, 1)
                    break

        except ValueError as e:
            import traceback
            traceback.print_stack()
            print(e)
            exit(-1)
        
            
    @staticmethod
    def show_dialog(parent=None):
        dialog = DisassemblyArchRadioDialog(parent)
        dialog.exec_()
        # DwarfConfig.disassembly_arch = DisassemblyArch(dialog.choice)

        